package com.task.springTask;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * spring task
 *
 * @author sunhui
 * @date 2018/4/26
 */
@Configuration
@EnableAsync
public class SpringTaskConfig {
    /**
     * 核心线程池大小
     */
    @Value("${com.springTask.corePoolSize}")
    private int corePoolSize;

    /**
     * 线程池最大书面
     */
    @Value("${com.springTask.maxPoolSize}")
    private int maxPoolSize;

    /**
     * 等待队列大小
     */
    @Value("${com.springTask.queueCapacity}")
    private int queueCapacity;

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }
}
